﻿$(document).on("pageInit", "#create-eva-teacher", function (e, id, page) {

    var $clazzIdSelector = $(page).find('[name="ClazzId"]');

    $.getJSON("/Evaluate/GetAllClazzEntity", {}, function (response) {
       
        $clazzIdSelector.empty();
        //$clazzIdSelector.append("<option>选择班级</option>");
        $.each(response, function (rIndex, rItem) {
            $clazzIdSelector.append("<option value='" + rItem.ClazzId + "'>" + rItem.ClazzName + "</option>");
        });
    });

    $clazzIdSelector.on("change", function () {
        loadStudent($clazzIdSelector.val());

    });

    loadCourceList();

    function loadCourceList() {
        $.getJSON("/CourceScore/GetCourceList", {}, function (response) {
            var $CourceId = $(page).find('[name="CourceId"]');
            $CourceId.empty();
            if (response.length > 0) {
                $.each(response, function (rIndex, rItem) {
                    $CourceId.append("<option value='" + rItem.CourceId + "'>" + rItem.CourceName + "</option>");
                });
                loadClazzList(response[0].CourceId);
            }
        });
    }

    function loadStudent(clazzId) {
        $.getJSON("/Evaluate/GetStudentListByClazzId", { clazzId: clazzId }, function (response) {
            var $StudentId = $(page).find('[name="StuUserId"]');
            $StudentId.empty();
            //$StudentId.append("<option>选择学生</option>");
            $.each(response, function (rIndex, rItem) {
                $StudentId.append("<option value='" + rItem.UserId + "'>" + rItem.RealName + "[学号：" + rItem.StuNo + "]</option>");
            });
        });
    }

    $(page).find("[name=create-eva-teacher-form]").on("submit", function (evt) {
        evt.preventDefault();
        $(page).find("[name=create-eva-teacher-form]").ajaxSubmit({
            success: function (response) {
                if (response.indexOf("err:") > -1) {
                    $.toast(response.substr(4));
                    return false;
                }
                history.back();
            }
        });
    });
});